<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

define('ROOT', '/etc/bh/');
define('NAMED', '/etc/bind/named.conf');
include ROOT."common.php";

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: Breizh Héberg Bot <robot@breizh-heberg.net>\n";
$headers .= "Reply-To: Support Breizh-Héberg <support@breizh-heberg.net>\n";

@unlink(NAMED);
$_named = fopen(NAMED, "a+");
fputs($_named,file_get_contents(ROOT."tpl/bind-primaire-modele.html"));

// Suppression des vieux domaines
query("DELETE FROM hebergdata_doms WHERE etat = 4 OR etat = 5");

// Utilisateurs / Alias en .breizh-web.net
	@unlink(ROOT."serveurs/data_bind/db.breizh-web.net");	$_db_bw = fopen(ROOT."serveurs/data_bind/db.breizh-web.net", "a+");
	fputs($_named,"\n".'zone "breizh-web.net" { type master; file "'.ROOT.'serveurs/data_bind/db.breizh-web.net"; };');
	$bind_serial = mysql_fetch_array(query("SELECT * FROM config WHERE nom='bind_current_serial'"));
	$_db_bw_content = array();
	$_db_bw_content['{domaine}'] = "breizh-web.net";
	$_db_bw_content['{serial}'] = date('Ymd').sprintf("%02d",intval($bind_serial['value']));
	fputs($_db_bw ,str_replace(array_keys($_db_bw_content), array_values($_db_bw_content), file_get_contents(ROOT."tpl/bind-zone-modele.html")));
	fputs($_db_bw,"\n"."@					IN				A				".$server[0]['ip']);
	fputs($_db_bw,"\n"."www					IN				A				".$server[0]['ip']);
	fputs($_db_bw,"\n"."*					MX      		1 				mail.breizh-heberg.net."."\n\n");
	$_liste_users = query("SELECT user_linux, serveur_apache, etat FROM comptes_heberg WHERE etat = 1");
	While($liste_users = mysql_fetch_array($_liste_users)) {
		fputs($_db_bw,"\n".$liste_users['user_linux']."				IN				A				".$server[$liste_users['serveur_apache']]['ip']);
		fputs($_db_bw,"\n"."www.".$liste_users['user_linux']."				IN				A				".$server[$liste_users['serveur_apache']]['ip']);
	}
	//fclose($_db_bw);

// Autres alias BH
reset($alias_bh);
while (list($id_alias, $ext_alias) = each($alias_bh)) {
	@unlink(ROOT."serveurs/data_bind/db.".$ext_alias);	$_db_bw = fopen(ROOT."serveurs/data_bind/db.".$ext_alias, "a+");
	fputs($_named,"\n".'zone "'.$ext_alias.'" { type master; file "'.ROOT.'serveurs/data_bind/db.'.$ext_alias.'"; };');
	$_db_alias_content = array();
	$_db_alias_content['{domaine}'] = $ext_alias;
	$_db_alias_content['{serial}'] = date('Ymd').sprintf("%02d",intval($bind_serial['value']));
	fputs($_db_bw ,str_replace(array_keys($_db_alias_content), array_values($_db_alias_content), file_get_contents(ROOT."tpl/bind-zone-modele.html")));
	fputs($_db_bw,"\n"."@					IN				A				".$server[0]['ip']);
	fputs($_db_bw,"\n"."www					IN				A				".$server[0]['ip']);
	fputs($_db_bw,"\n"."*					MX      		1 				mail.breizh-heberg.net."."\n\n");
	$_recup_entrees = query("SELECT 
							hebergdata_doms.domaine, 
							hebergdata_doms.id_alias_bh,
							hebergdata_doms.id_heberg,
							comptes_heberg.id,
							comptes_heberg.serveur_apache
							FROM hebergdata_doms
							LEFT JOIN comptes_heberg
							ON hebergdata_doms.id_heberg = comptes_heberg.id
							WHERE hebergdata_doms.id_alias_bh = '$id_alias'");
	While($recup_entree = mysql_fetch_array($_recup_entrees)) {
		fputs($_db_bw,"\n".$recup_entree['domaine']."				IN				A				".$server[$recup_entree['serveur_apache']]['ip']);
		fputs($_db_bw,"\n"."www.".$recup_entree['domaine']."		IN				A				".$server[$recup_entree['serveur_apache']]['ip']);
		query("UPDATE hebergdata_doms SET etat = '1' WHERE domaine = '".$recup_entree['domaine']."' AND id_alias_bh = '$id_alias'");
	}
	//fclose($_db_bw);
}

// Domaines Clients externes
$_liste_doms_h = query("SELECT
							hebergdata_doms.id AS d_id,
							hebergdata_doms.id_dom_bh,
							hebergdata_doms.domaine,
							hebergdata_doms.etat,
							hebergdata_doms.id_alias_bh,
							hebergdata_doms.id_heberg,
							comptes_heberg.id,
							comptes_heberg.serveur_apache
							FROM hebergdata_doms 
							LEFT JOIN comptes_heberg
							ON hebergdata_doms.id_heberg = comptes_heberg.id
							WHERE hebergdata_doms.id_alias_bh = 0 
							AND hebergdata_doms.id_dom_bh = 0");
While($liste_doms_h = mysql_fetch_array($_liste_doms_h)) {
	if($liste_doms_h['etat'] == 3 OR $liste_doms_h['etat'] == 1 OR $liste_doms_h['etat'] == 6) {
		fputs($_named,"\n".'zone "'.$liste_doms_h['domaine'].'" { type master; file "'.ROOT.'serveurs/data_bind/db.'.$liste_doms_h['domaine'].'"; };');
	}
	//if((!file_exists(ROOT."serveurs/data_bind/db.".$liste_doms_h['domaine'])) OR ($liste_doms_h['etat'] == 3)) {
		@unlink(ROOT."serveurs/data_bind/db.".$liste_doms_h['domaine']);
		$_zdns = fopen(ROOT."serveurs/data_bind/db.".$liste_doms_h['domaine'], "a+");
		$_db_domclient_content = array();
		$_db_domclient_content['{domaine}'] = $liste_doms_h['domaine'];
		$_db_domclient_content['{serial}'] = date('Ymd').sprintf("%02d",intval($bind_serial['value']));
		fputs($_zdns,str_replace(array_keys($_db_domclient_content), array_values($_db_domclient_content), file_get_contents(ROOT."tpl/bind-zone-modele.html")));
		fputs($_zdns,"\n"."@					MX      		1 				mail.breizh-heberg.net.");
		fputs($_zdns,"\n"."@					IN				A				".$server[$liste_doms_h["serveur_apache"]]['ip']);
		fputs($_zdns,"\n"."www					IN				A				".$server[$liste_doms_h["serveur_apache"]]['ip']);
		fputs($_zdns,"\n"."ftp					IN				A				".$server[$liste_doms_h["serveur_apache"]]['ip']);
		$_ssdoms = query("SELECT * FROM hebergdata_ssdoms WHERE id_dom = '".$liste_doms_h['d_id']."'");
		While($ss_doms = mysql_fetch_array($_ssdoms)) {
			fputs($_zdns,"\n".$ss_doms['ss_dom']."					IN				A				".$server[$liste_doms_h["serveur_apache"]]['ip']);
		}		
		if($liste_doms_h['etat'] == 3) {
			query("UPDATE hebergdata_doms SET etat = 6 WHERE id = '".$liste_doms_h['d_id']."'");
		}
	//}
}

//Domaines achetés sur BH
$_liste_doms_h = query("SELECT
							zones_dns.id as z_id,
							zones_dns.id_domain,
							zones_dns.etat,
							comptes_domain.id,
							comptes_domain.domaine
							FROM zones_dns
							LEFT JOIN comptes_domain
							ON zones_dns.id_domain = comptes_domain.id
							");
While($liste_doms_h = mysql_fetch_array($_liste_doms_h)) {

	if(($liste_doms_h['etat'] == 3 OR $liste_doms_h['etat'] == 1) AND ($liste_doms_h['domaine'] != '')){
		fputs($_named,"\n".'zone "'.$liste_doms_h['domaine'].'" { type master; file "'.ROOT.'serveurs/data_bind/db.'.$liste_doms_h['domaine'].'"; };');
	}
	//if(!file_exists(ROOT."serveurs/data_bind/db.".$liste_doms_h['domaine']) OR $liste_doms_h['etat'] == 3 OR $liste_doms_h['etat'] == 0) {
		unlink(ROOT."serveurs/data_bind/db.".$liste_doms_h['domaine']);
		$_zdns = fopen(ROOT."serveurs/data_bind/db.".$liste_doms_h['domaine'], "a+");
		$_db_domclient_content = array();
		$_db_domclient_content['{domaine}'] = $liste_doms_h['domaine'];
		$_db_domclient_content['{serial}'] = date('Ymd').sprintf("%02d",intval($bind_serial['value']));
		fputs($_zdns,str_replace(array_keys($_db_domclient_content), array_values($_db_domclient_content), file_get_contents(ROOT."tpl/bind-zone-modele.html")));
		$_entrees = query("SELECT * FROM zones_dns_content WHERE id_zone = '".$liste_doms_h['z_id']."'");
		While($entrees = mysql_fetch_array($_entrees)) {
			if (preg_match('#^([0-9.]+)$#', $entrees['value'])) 
			{
				$value = $entrees['value'];
			}
			else {
				$value = $entrees['value'].".";
			}
 
			if($entrees['type'] ==  "A" OR $entrees['type'] ==  "CNAME") { $in = "IN			".$entrees['type']; } else { $in = "MX			".$entrees['value_mx'];  }
			fputs($_zdns,"\n".$entrees['entree']."					$in							".$value);
		}
		query("UPDATE zones_dns SET etat = 1 WHERE id_domain = '".$liste_doms_h['id_domain']."'");
	//}
}

shell_exec("sudo /etc/init.d/bind9 restart");
query("UPDATE config SET value = value + 1 WHERE nom='bind_current_serial'");
deconnexionbdd();
?>
